<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\middleware\admin\Auth;
use app\model\Admins;
use app\model\Role;
use app\model\Rule as RuleModel;
class Rule extends BaseController
{
    protected $middleware=[Auth::class];
    public function index()
    {
        $role = Admins::with('role')->find(request()->userid);
        if ($role->role_id == 1) {
            $menu = RuleModel::order('sort', 'desc')->select();

        } else {
            $rule = Role::find($role->role_id);
            $menu = RuleModel::order('sort', 'desc')->whereIn('id', $rule->rule_id)->select();
        }
        $power['list']=getTree($menu);
        $power['rules']= getTree(RuleModel::where('is_menu', 1)->order('sort', 'desc')->select());
        return $this->success($power);
    }


    public function create()
    {

        return $this->success(RuleModel::create(request()->param()));
    }



    public function update($id)
    {
        return $this->success(RuleModel::where('id',$id)->update(request()->param()));
    }


    public function delete($id)
    {
        return $this->success(RuleModel::where('id',$id)->delete());
    }
}
